package com.atguigu.gulimall.auth.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

/**
 * SpringSession 核心原理
 * <p>
 * 自动延期
 * <p>
 * 1.@EnableRedisHttpSession 导入了 RedisHttpSessionConfiguration 配置
 * 1、给容器中添加了 RedisIndexedSessionRepository 组件：redis操作session
 * 2、父类给容器中添加了 SessionRepositoryFilter 组件：session存储的过滤器，每个请求都经过filter
 * 1、创建的时候就自动的从容器中获取到了 [RedisIndexed]SessionRepository
 * 2、原生的 request 和 response 都被包装成了 SessionRepositoryRequestWrapper 和 SessionRepositoryResponseWrapper
 * 3、以后获取 session：[SessionRepositoryRequestWrapper]request.getSession();====>SessionRepository 中获取的session
 * <p>
 * 装饰者模式
 */

@Configuration
@EnableRedisHttpSession //整合redis作为session存储
public class GulimallSessionConfig {
    /**
     * 设置 cookie名字和作用域
     *
     * @return
     */
    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
        cookieSerializer.setCookieName("GULISESSION");
        cookieSerializer.setDomainName("gulimall.com");
        return cookieSerializer;
    }

    /**
     * json格式串行化
     *
     * @return
     */
    @Bean
    public RedisSerializer springSessionDefaultRedisSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }
}
